
/**
 * AppleIIGo
 * Resources class
 * (C) 2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Resources.h"
#include "fileutilities.h"

Resources::Resources(char * resourcesPath) {
	this->resourcesPath = strdup(resourcesPath);
}

Resources::~Resources() {
	if (resourcesPath)
		free(resourcesPath);
}

UINT32 Resources::getResourceByteNum(char * resourceName) {
	UINT32 resourceByteNum = 0;
	char * resourcePath = getResourcePath(resourceName);
	if (!resourcePath)
		return 0;
	resourceByteNum = getFileByteNum(resourcePath);
	free(resourcePath);
	
	return resourceByteNum;
}

UINT32 Resources::readResource(UINT8 * buffer, UINT32 bufferByteNum,
							   char * resourceName) {
	UINT32 readByteNum = 0;
	char * resourcePath = getResourcePath(resourceName);
	if (!resourcePath)
		return 0;
	
	FILE * fp = fopen(resourcePath, "rb");
	if (fp) {
		readByteNum = fread(buffer, 1, bufferByteNum, fp);
		fclose(fp);
	}
	
	free(resourcePath);
	
	return readByteNum;
}

UINT32 Resources::readWaveResource(SINT16 * buffer, UINT32 sampleNum,
								   char * resourceName) {
	return 0;
}

char * Resources::getResourcePath(char * resourceName) {
	if (!resourcesPath)
		return 0;
	
	char * resourcePath;
	asprintf(&resourcePath, "%s/%s", resourcesPath, resourceName);
	
	return resourcePath;
}
